package com.kang.until;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.kang.pojo.IntemInformation;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 系统统计模块PDF下载
 * @author zs
 *
 */
public class SystemStatisticsPDF {


	/**
	 * 字体
	 */
	public static BaseFont bf = null;
	private static com.lowagie.text.Font fontChinese_14;
	private static BaseFont bfChinese;
	private static com.lowagie.text.Font fontChinese_16;
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 初始化字体
	 */
	static {
		try {
			bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建表格统一单元格样式
	 * 
	 * @param str
	 * @return
	 */
	public static Cell createTableCell(String str, Font font) {
		Cell cell = null;
		try {
			cell = new Cell(new Phrase(str, font));
			cell.setBorderColor(new Color(0, 0, 0));
			// 单元格水平对齐方式
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			// 单元格垂直对齐方式
			cell.setVerticalAlignment(Element.ALIGN_CENTER);
		} catch (BadElementException e) {
			e.printStackTrace();
		}
		return cell;
	}

	/**
	 * 创建表格并生成表头
	 * 
	 * @param title
	 * @param titleFont
	 * @param tableHeader
	 * @param headerFont
	 * @return
	 * @throws Exception
	 */
	public static Table createTable(String title, Font titleFont, String[] tableHeader, Font headerFont)
			throws Exception {
		Table table = new Table(tableHeader.length);
		table.setBorderWidth(1);
		table.setBorderColor(new Color(0, 0, 0));
		table.setPadding(1);
		table.setSpacing(0);
		table.setWidth(100);
		// 表头说明
		Cell cell = createTableCell(title, titleFont);
		cell.setHeader(true);
		cell.setColspan(tableHeader.length);
		table.addCell(cell);
		// 表头
		for (int i = 0; i < tableHeader.length; i++) {
			cell = createTableCell(tableHeader[i], headerFont);
			table.addCell(cell);
		}
		// 添加此代码后超出每页都会显示表头
		table.endHeaders();
		return table;
	}

	/**
	 * 设置表格数据
	 * 
	 * @param table
	 * @param columns
	 * @param values
	 * @param baseFont
	 * @throws Exception
	 */
	public static void setTableValue(Table table, String[] columns, List<?> values, Font baseFont) throws Exception {
		// 表格
		int length = table.getColumns();
		//System.out.println("==values==="+ values.size());
		//System.out.println("==columns==="+ columns.length);
		if (values != null) {
			for (int i = 0; i < values.size(); i++) {
				Object obj = values.get(i);
				//System.out.println("==obj==="+ obj.getClass());
				for (int j = 0; j < length; j++) {
					if (columns.length <= j) {
						table.addCell(createTableCell("", baseFont));
						continue;
					}
					String field = columns[j];
					String value = "";
					if (obj instanceof Map) {
						value = ((Map<?, ?>) obj).get(field).toString();
					} else {
						String methodName = "get" + field.substring(0, 1).toUpperCase() + field.substring(1);
						Method getterMethod = obj.getClass().getMethod(methodName);
						Object valueObj = getterMethod.invoke(obj);
						if (valueObj != null)
							value = valueObj.toString();
					}
					table.addCell(createTableCell(value, baseFont));
				}
			}
		}
	}
	
	/**
	 * 设置单元格格式
	 * @param cell 单元格
	 * @return
	 */
	public static Cell setInfoCellStyle(Cell cell) {
		cell.setBorderColor(new Color(0, 0, 0));
		// 单元格水平对齐方式
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		// 单元格垂直对齐方式
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		return cell;
	}
	/**
	 * 创建表格统一单元格样式
	 * @param str
	 * @return
	 */
	public static Cell createCell(String str,com.lowagie.text.Font font){
		Cell cell = null;
		try {
			cell = new Cell(new Phrase(str,font));
			cell.setBorderColor(new Color(0, 0, 0));
			 //单元格水平对齐方式
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        //单元格垂直对齐方式
	        cell.setVerticalAlignment(Element.ALIGN_CENTER);
		} catch (BadElementException e) {
			e.printStackTrace();
		}
		return cell;
	}
	
//==============================================采购统计==================================================================	

	/**
	 * 生成采购统计PDF文件
	 * @param request
	 * @param items
	 * @return
	 */
	public static Table buildPurchasingStatisticsTable(List<Map<String, String>> iss) throws BadElementException {
		
		Table table = new Table(6);
		table.setBorderWidth(1);
		table.setBorderColor(new Color(0, 0, 0));
		table.setPadding(1);
		table.setSpacing(0);
		table.setWidth(100);
		//表头说明
		Cell cell = createCell("采购统计",fontChinese_14);
		cell.setHeader(true);
		cell.setColspan(6);
		table.addCell(cell);
		//cell.setRowspan(2);跨行
		//cell.setColspan(2);跨列
		//表头
		String[] tableHeader = new String[]{ "物品代码", "条码", "名称", "仓库", "数量", "处理时间"};
		List<Cell> tableHeaderCellList = new ArrayList<Cell>();
		for (int i = 0; i < tableHeader.length; i++) {
			cell = createCell(tableHeader[i],fontChinese_14);
			tableHeaderCellList.add(cell);
		}
		for (int i = 0; i < tableHeader.length; i++) {
			table.addCell(tableHeaderCellList.get(i));
		}
		// 添加此代码后超出每页都会显示表头
		table.endHeaders();
		//表格
		if(iss!=null){
			int listSize=iss.size();
			for (int i = 0; i < listSize; i++) {
				//判断是否存在字段
				String str;
				Boolean bl = iss.get(i).containsKey("number");
				if (bl){
					str = String.valueOf(iss.get(i).get("number"));
				}else{
					str = " ";
				}
				
				Cell cell1 = createCell(iss.get(i).get("itemCode"),fontChinese_14);
				Cell cell2 = createCell(iss.get(i).get("articleBarCode"),fontChinese_14);
				Cell cell3 = createCell(iss.get(i).get("chineseName"),fontChinese_14);
				Cell cell4 = createCell(iss.get(i).get("warehouseName"),fontChinese_14);
				Cell cell5 = createCell(str,fontChinese_14);
				Cell cell6 = createCell(sdf.format(iss.get(i).get("lastModifiedTime")).toString(),fontChinese_14);
				
				table.addCell(cell1);
				table.addCell(cell2);
				table.addCell(cell3);
				table.addCell(cell4);
				table.addCell(cell5);
				table.addCell(cell6);
			}
		}
		
		return table;
	}
	
	/**
	 * 生成采购入库PDF单
	 * 
	 * @return
	 * @throws Exception
	 */
	public static File PurchasingStatisticsPDF(List<Map<String, String>> iss){

		try {
			bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		}catch (DocumentException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		fontChinese_14 = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
		fontChinese_16 = new com.lowagie.text.Font(bfChinese, 16, com.lowagie.text.Font.NORMAL);
		
		// Rectangle pageSize = new Rectangle(270, 300);//设置文档宽高
		Document document = new Document(PageSize.A4,10, 10, 10, 10);// 分别为文档的大小、左、右、上、下的页边距
		//System.out.println("======================"+document.getPageSize().getWidth());
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
		String date = sdf2.format(new Date());
		File pdfFile = new File(FileUtil.TEMP_PATH +"采购统计_"+date+".pdf");
		try {
			PdfWriter.getInstance(document,new FileOutputStream(pdfFile.getAbsolutePath()));
		} catch (FileNotFoundException | DocumentException e1) {
			e1.printStackTrace();
		}
		
		document.open();

		try {
			Paragraph title = new Paragraph("采购统计", fontChinese_16);
			title.setAlignment(1);// 居中对齐
			document.add(title);
			document.add(buildPurchasingStatisticsTable(iss));
			document.add(new Paragraph(" ", fontChinese_14));
			document.add(new Paragraph("打印时间："+sdf.format(new Date()), fontChinese_14));
			document.add(new Paragraph(" ", fontChinese_14));
			document.add(new Paragraph("负责人签名：", fontChinese_14));
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		document.close();
		return pdfFile;
	}

}
